package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.base.MultiItemCommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.MultiItemTypeSupport;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.model.CreditDetailSeries;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * 信用卡详情适配器
 * Created by zad
 * 2016/11/21 15:17
 */

public class CreditDetailNewAdapter extends MultiItemCommonAdapter<CreditDetailSeries> {
    /**
     * 信用卡详情页面当前显示的信用卡名称
     */
    private String cardName;
    /**
     * 猜你喜欢换一批部分
     */
    private List<CreditDetailSeries> mCreditLikeSwitchOneList;
    /**
     * 猜你喜欢换一批部分
     */
    private List<CreditDetailSeries> mCreditLikeSwitchList;

    /**
     * 多item类型通用适配器
     * @param mCreditLikeSwitchList 猜你喜欢换一批列表
     */
    public CreditDetailNewAdapter(Context mContext, List<CreditDetailSeries> mData, List<CreditDetailSeries> mCreditLikeSwitchList) {
        super(mContext, mData, new MultiItemTypeSupport<CreditDetailSeries>() {
            @Override
            public int getLayoutId(int position, CreditDetailSeries credit) {
                return R.layout.layout_credit_detail_item_new;
            }

            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public int getItemViewType(int position, CreditDetailSeries credit) {
                return 0;
            }
        });
        this.mCreditLikeSwitchList = mCreditLikeSwitchList;
        this.mCreditLikeSwitchOneList = mData;

        mDividerDrawable = ContextCompat.getDrawable(mContext, R.drawable.shape_item_divider);
    }

    public void setDataSource(List<CreditDetailSeries> mData,List<CreditDetailSeries> mCreditLikeSwitchList) {
        this.mData = mData;
        this.mCreditLikeSwitchList = mCreditLikeSwitchList;
        this.mCreditLikeSwitchOneList = mData;
    }

    private Drawable mDividerDrawable = null;

    @Override
    public void convert(ViewHolder holder, final CreditDetailSeries item, final int position) {
        switch (holder.getLayoutId()) {
            case R.layout.layout_credit_detail_item_new:
                LinearLayout container = holder.getView(R.id.id_ly_item_container);
                container.setDividerDrawable(null);
                LinearLayout ll_parent = holder.getView(R.id.id_ll_parent);
                ll_parent.setDividerDrawable(null);
                if (position == 0 || item.getTypeId() != mData.get(position - 1).getTypeId()) {
                    //显示类型
                    ll_parent.setDividerDrawable(mDividerDrawable);
                    holder.getView(R.id.id_ll_type).setVisibility(View.VISIBLE);
                    holder.setText(R.id.id_tv_type_name, item.getTypeName());
                    if (item.getTypeId() == 2) {
                        //显示猜你喜欢右侧的“换一批”字样
                        holder.getView(R.id.id_ll_switch).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //切换 换一批，查看更多
                                switchYouLikeList(item, position);
                            }
                        });
                        holder.setText(R.id.id_tv_right, "换一批");//换一批
                    } else {
                        //同系列不显示“换一批”字样
                        holder.getView(R.id.id_ll_switch).setVisibility(View.GONE);
                    }
                } else {
                    //隐藏类型
                    holder.getView(R.id.id_ll_type).setVisibility(View.GONE);
                }
                //绑定数据
                holder.setText(R.id.id_tv_title, item.getName());//标题
                holder.setText(R.id.id_tv_describe, item.getCreditLineMin() + "-" + item.getCreditLineMax() + "额度，" + item.getWithdrawalLimit() + "%取现额");//描述
                //设置申请人数
                int applyCount = 0;
                if (item.getApplyCountDIY() > 0) {//假数据大于0 则显示假数据
                    applyCount = item.getApplyCountDIY();
                } else {//显示真数据
                    applyCount = item.getApplyCount();
                }
                holder.setText(R.id.id_tv_apply_num, applyCount + "");
                holder.displayCornerImage(R.id.id_iv_bank_logo, URLUtil.CBB_IMG_URL + item.getImage(), R.drawable.icon_default_credit);
                break;
        }
    }

    /**
     * 将信用卡详情页面正在展示的信用卡名字传进来
     */
    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    /**
     * 猜你喜欢列表内容切换
     */
    private void switchYouLikeList(CreditDetailSeries item, int position) {
        if (item.getSwitchTipState() == 0) {
            //换一批
            int len = mData.size();
            while (position <= len - 1 && len > 0) {
                mData.remove(position);
                len = mData.size();
            }
            mData.addAll(mCreditLikeSwitchList);
            mData.get(position).setSwitchTipState(1);//表示查看更多
            notifyDataSetChanged();
        } else {
            mData.clear();
            mData.addAll(mCreditLikeSwitchOneList);
            mData.get(position).setSwitchTipState(0);//表示查看更多
            notifyDataSetChanged();
        }
    }
}
